home *** CD-ROM | disk | FTP | other *** search
- Path: news.logicon.com!newsmaster@klee
- From: kkolda@logicon.com (Kenneth D. Kolda)
- Newsgroups: comp.lang.c++
- Subject: Re: Need help with an array of pointers
- Date: 26 Mar 1996 17:57:30 GMT
- Organization: Logicon Operating Systems
- Message-ID: <4j9b6a$num@piper.logicon.com>
- References: <3157FBAF.32A3@village.ios.com>
- NNTP-Posting-Host: 137.51.122.161
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.2
-
- In article <3157FBAF.32A3@village.ios.com>, kboruff@village.ios.com
- says...
- >
- >Suppose I have an array of pointers:
- >
- >char *WordPtr[] = {"Jack", "Joe", "Bob"};
- >
- >How do I print the address value of each pointer element (i.e., the
- >address that each pointer points to)? The only addresses I seem to be
- >able to print are the addresses of the array elements themselves.
- >
- >I would appreciate any help on this subject. An array of pointers is a
- >confusing topic to me and I could use all the help I can get.
- >
- >Keith Boruff
- >Long Island, NY
-
- Let's make a sample setup in memory:
-
- Variable Name Address(es) Value
- ------------- ----------- -----
-
- WordPtr[0] 0x10 0x100
- WordPtr[1] 0x11 0x200
- WordPtr[2] 0x12 0x300
- 0x100-0x104 'J', 'a', 'c', 'k', '\0'
- 0x200-0x203 'J', 'o', 'e', '\0'
- 0x300-0x303 'B', 'o', 'b', '\0'
-
-
- As you can see, WordPtr[0] points to the name "Jack", WordPtr[1] points
- to "Joe" and WordPtr[2] points to "Bob".
-
- Now consider each of the following values:
-
- 1) (int) WordPtr == (int) &WordPtr[0] = 0x10
-
- This returns the address of the array, which is also the
- address of the 0th element of the array (which is why
- we can use WordPtr[i] and *(WordPtr + i) interchangably).
-
- 2) (int) &WordPtr[1] = 0x11
-
- The address of the 1st element of the array WordPtr. This is
- what you're getting that you do *not* want.
-
- 3) (int) WordPtr[1] = (int) &WordPtr[1][0] = 0x200
-
- This is the value of WordPtr[1] interpreted as an integer, i.e.
- what is the value of the pointer in WordPtr[1]?? This is what
- you're looking for. The second interpretation is: What is the
- address of the 0th element of the string pointed to be
- WordPtr[1]?? These are equivalent statements.
-
- Hope that helps!
-
- Ken Kolda
-
-